<?php
namespace app\common\service;

class Http
{
    public function get($url, $fields = array()) {

        return $this->curl($url,"GET",$fields);
    }

    public function post($url, $fields = array()) {

        return $this->curl($url,"POST", $fields);
    }

    private function curl($url, $method, $fields = NULL)
    {
        $http_info = array();
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);

        switch ($method) {
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, TRUE);
                if (!empty($fields)) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
                }
                break;
            case 'DELETE':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($fields)) {
                    $url = "{$url}?{$fields}";
                }
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        $response = curl_exec($curl);
        curl_close ($curl);
        return $response;
    }
     
}


?>